package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.wx.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.wx.WxCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

@Service
public class WxCartServiceImpl implements WxCartService {

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    @Transactional
    public Integer addCart(Integer id, WxCartAddBo wxCartAddBo) {

        MarketCart marketCart1 = marketCartMapper.selectByIdAndgoodsId(id, wxCartAddBo.getGoodsId());
        if (marketCart1 != null) {
            int num = wxCartAddBo.getNumber() + marketCart1.getNumber();
            marketCartMapper.updateNumber(marketCart1.getId(), num);
            return marketCart1.getId();
        }

        MarketGoodsProduct product = marketGoodsProductMapper.selectByPrimaryKey(wxCartAddBo.getProductId());
        MarketGoods orderGoods = marketGoodsMapper.selectByPrimaryKey(wxCartAddBo.getGoodsId());
        MarketCart marketCart = new MarketCart();
        marketCart.setUserId(id);
        marketCart.setGoodsId(product.getGoodsId());
        marketCart.setGoodsSn(String.valueOf(product.getGoodsId()));
        marketCart.setGoodsName(orderGoods.getName());
        marketCart.setProductId(product.getId());
        marketCart.setPrice(orderGoods.getRetailPrice());
        marketCart.setNumber(wxCartAddBo.getNumber());
        marketCart.setSpecifications(product.getSpecifications());
        marketCart.setPicUrl(product.getUrl());
        marketCart.setAddTime(new Date());
        marketCart.setUpdateTime(new Date());
        marketCartMapper.insertSelective(marketCart);
        return marketCart.getId();
    }

    @Override
    @Transactional
    public CartIndexVo index(Integer userId) {
        ArrayList<MarketCart> marketCarts = marketCartMapper.selectCartById(userId);
        CartIndexVo cartIndexVo = new CartIndexVo();
        cartIndexVo.setCartList(marketCarts);
//        {goodsCount: 8, checkedGoodsCount: 3, goodsAmount: 7524, checkedGoodsAmount: 1297}
        int goodsCount = 0;
        int checkedGoodsCount = 0;
        BigDecimal goodsAmount = BigDecimal.valueOf(0);
        BigDecimal checkedGoodsAmount = BigDecimal.valueOf(0);

        BigDecimal count = BigDecimal.valueOf(0);
        BigDecimal count2 = BigDecimal.valueOf(0);
        for (MarketCart marketCart : marketCarts) {
            BigDecimal bigDecimal = new BigDecimal(marketCart.getNumber());
            if (marketCart.getChecked()) {
                count = marketCart.getPrice().multiply(bigDecimal);
                checkedGoodsAmount = count.add(checkedGoodsAmount);
                checkedGoodsCount = marketCart.getNumber() + checkedGoodsCount;
            }
            count2 = marketCart.getPrice().multiply(bigDecimal);
            goodsCount = marketCart.getNumber() + goodsCount;
            goodsAmount = count2.add(goodsAmount);
        }

        CartTotalVo cartTotalVo = new CartTotalVo();
        cartTotalVo.setCheckedGoodsAmount(checkedGoodsAmount);
        cartTotalVo.setCheckedGoodsCount(checkedGoodsCount);
        cartTotalVo.setGoodsAmount(goodsAmount);
        cartTotalVo.setGoodsCount(goodsCount);
        cartIndexVo.setCartTotal(cartTotalVo);
        return cartIndexVo;
    }

    @Override
    @Transactional
    public Integer checked(WxCartCheckedBo wxCartCheckedBo) {
        String[] productIds = wxCartCheckedBo.getProductIds();
        boolean b = true;
        if (wxCartCheckedBo.getIsChecked() != 1) {
            b = false;
        }
        for (String productId : productIds) {
            marketCartMapper.updateChecked(productId, b);
            System.out.println(wxCartCheckedBo.getIsChecked());
        }
        return 0;
    }

    @Override
    public Integer update(MarketCart marketCart) {
        int i = marketCartMapper.update(marketCart);
        return i;
    }

    @Override
    public Integer goodscount(int id) {
        Integer i = marketCartMapper.selectByUserId(id);
        if (i == null){
            return 0;
        }
        return i;
    }

    @Override
    public Integer delete(Integer userId, WxCartCheckedBo wxCartCheckedBo) {
        String[] productIds = wxCartCheckedBo.getProductIds();
        for (String productId : productIds) {
            marketCartMapper.deleteByUserId(userId, productId);
        }
        return 0;
    }

    //cartId: 0
    //addressId: 2 地址表id
    //couponId: 54 优惠卷id
    //userCouponId: 91 优惠卷的用户id
    //grouponRulesId: 0
    @Override
    @Transactional
    public WxCartCheckoutVo checkout(Integer userId, Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId,Short goodsNum) {
        WxCartCheckoutVo wxCartCheckoutVo = new WxCartCheckoutVo();
        ArrayList<MarketCart> marketCarts = null;
        BigDecimal goodsTotalPrice = BigDecimal.valueOf(0);
        if (cartId == 0){
            marketCarts = marketCartMapper.selectByIdAndCheck(userId);

        }else {
             marketCarts = marketCartMapper.selectByCartId(cartId);

        }
        wxCartCheckoutVo.setCheckedGoodsList(marketCarts);
        BigDecimal count = null;
        Short num = 0;
        for (MarketCart marketCart : marketCarts) {
            if (cartId != 0){
                num = goodsNum;
            }else {
                num = marketCart.getNumber();
            }
            BigDecimal bigDecimal = new BigDecimal(num);
            count = marketCart.getPrice().multiply(bigDecimal);
            goodsTotalPrice =count.add(goodsTotalPrice);
        }

        MarketAddress marketAddress = null;
        if (addressId == 0){
             marketAddress  =  marketAddressMapper.selectByDefalut();
        }else {
            marketAddress = marketAddressMapper.selectByPrimaryKey(addressId);
        }

        if(marketAddress == null){
            MarketAddress marketAddress1 = new MarketAddress();
            marketAddress1.setId(0);
            wxCartCheckoutVo.setCheckedAddress(marketAddress1);
            wxCartCheckoutVo.setAddressId(0);
        }else {
            wxCartCheckoutVo.setAddressId(marketAddress.getId());
            wxCartCheckoutVo.setCheckedAddress(marketAddress);
        }



        wxCartCheckoutVo.setCartId(cartId);
        wxCartCheckoutVo.setCouponId(couponId);
        BigDecimal actualPrice = null;
        BigDecimal a = BigDecimal.valueOf(0);
        if (couponId == -1) {
            wxCartCheckoutVo.setCouponPrice(BigDecimal.valueOf(0));
            actualPrice = goodsTotalPrice;
        }else if (couponId == 0){
            wxCartCheckoutVo.setCouponPrice(BigDecimal.valueOf(0));
            actualPrice = goodsTotalPrice;
        }else{
            MarketCoupon marketCoupon = marketCouponMapper.selectByCouponId(couponId);
            wxCartCheckoutVo.setCouponPrice(marketCoupon.getDiscount());
            actualPrice = goodsTotalPrice.subtract(marketCoupon.getDiscount());
            int flag = actualPrice.compareTo(a);
            if (flag == -1){
                actualPrice = BigDecimal.valueOf(0);
            }
        }
        wxCartCheckoutVo.setActualPrice(actualPrice);
        wxCartCheckoutVo.setFreightPrice(0);
        wxCartCheckoutVo.setGoodsTotalPrice(goodsTotalPrice);
        wxCartCheckoutVo.setGrouponPrice(0);
        wxCartCheckoutVo.setGrouponRulesId(0);
        wxCartCheckoutVo.setOrderTotalPrice(actualPrice);
        wxCartCheckoutVo.setUserCouponId(userCouponId);
        Integer availableCouponLength = 0;
        ArrayList<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByCouponUserId(userId);
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            MarketCoupon marketCoupon = marketCouponMapper.selectByCouponUserId(marketCouponUser.getCouponId());
            if (actualPrice.compareTo(marketCoupon.getMin()) == 1 ){
                availableCouponLength++;
            }
        }
        wxCartCheckoutVo.setAvailableCouponLength(availableCouponLength);
        return wxCartCheckoutVo;
    }

}
